package com.driver.manage.utils;
import com.alibaba.fastjson.JSONObject;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
/**
 * @author 86131
 * @title DecryptOCRResult
 * @date 2023/6/19 20:05
 * @description TODO
 */
public class DecryptOCRResult {

    public static JSONObject getString(String str) throws Exception {
        // 假设 result 是获取的 OCR 识别结果密文
        String result = str;
        byte[] resultBytes = Base64.getDecoder().decode(result);

        // AES解密需要的Key和IV，假设都是一些默认值
        byte[] keyBytes = "0123456789abcdef".getBytes();
        byte[] ivBytes = "0123456789abcdef".getBytes();

        // 创建SecretKeySpec对象，并使用AES算法生成密钥
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");

        // 创建IvParameterSpec对象，表示初始化向量
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

        // 使用Cipher进行AES解密
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(resultBytes);
        String decryptedResult = new String(decryptedBytes, "UTF-8");

        // 将解密后的结果转换成JSONObject
        JSONObject jsonResult = JSONObject.parseObject(decryptedResult);

        // 输出解密后的识别结果
        System.out.println(jsonResult.toJSONString());
        return jsonResult;
    }
}

